package com.yantong.service.impl;

import com.yantong.utils.PageUtils;
import com.yantong.service.PayService;
import com.yantong.pojo.AssetConfig;
import com.yantong.service.ConfigService;
import com.yantong.pojo.*;
import com.yantong.enums.MemberRoleEnum;
import com.yantong.enums.WalletLogOperationTypeEnum;
import com.yantong.enums.WalletLogTypeEnum;
import com.yantong.mapper.MemberPartnerMapper;
import com.yantong.service.MemberPartnerService;
import com.yantong.service.MemberRoleService;
import com.yantong.service.MemberService;
import com.yantong.service.MemberWalletService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 合伙人申请资料
 *
 * @author liaozhaogui-773104632@qq.com
 * @date 2020/10/13 17:47
 */
@Service
public class MemberPartnerServiceImpl implements MemberPartnerService {
	@Autowired
	private MemberPartnerMapper memberPartnerMapper;
	@Autowired
	private MemberRoleService memberRoleService;
	@Autowired
	@Lazy
	private PayService payService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private ConfigService configService;
	@Autowired
	private MemberWalletService memberWalletService;

	@Override
	public Long insert(MemberPartner memberPartner) {
		MemberPartner memberPartnerParam = new MemberPartner();
		memberPartnerParam.setMid(memberPartner.getMid());
		memberPartnerParam.setAuditStatus(0);
		List<MemberPartner> list = memberPartnerMapper.selectByMid(memberPartnerParam);
		if (CollectionUtils.isNotEmpty(list)) {
			for (MemberPartner memberPartner1 : list) {
				memberPartnerMapper.deleteById(memberPartner1.getId());
			}
		}
		return memberPartnerMapper.insert(memberPartner);
	}

	@Override
	public PageUtils<MemberPartner> listMemberPartners(ListMemberParams listMemberParams, MemberPartner memberPartner, int offset, int limit) {
		PageUtils<MemberPartner> pageUtils = new PageUtils<>();
		HashMap<String, Object> searchMap = new HashMap<>(8);

		if (memberPartner.getAuditStatus() != null) {
			searchMap.put("auditStatus", memberPartner.getAuditStatus());
		}
		if (StringUtils.isNotEmpty(listMemberParams.getKeyword())) {
			searchMap.put("keyword", listMemberParams.getKeyword());
		}

		searchMap.put("ORDERLIST", "t.application_date desc,audit_status asc");
		int total = memberPartnerMapper.getCountByWhere(searchMap);
		if (total > 0) {
			pageUtils.setTotal(total);
			List<MemberPartner> memberList = memberPartnerMapper.getListByWhere(searchMap, new RowBounds(offset, limit));
			pageUtils.setRows(memberList);
		} else {
			pageUtils.setRows(new ArrayList<>());
		}
		return pageUtils;
	}

}
